﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;               //数据库头文件
using System.Data.SqlClient;    //数据库头文件
using System.Data.Sql;         //数据库头文件
using DATA_operate_;          //自定义数据库操作类引用



public partial class design_page_admin_Default : System.Web.UI.Page
{
    DATA_operate data_operate_in_admin = new DATA_operate();   //创建数据库操作类对象
 
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {

            //主表数据绑定
            /*sql查询语句
            select p1._Cid,_Cname,p3._Tid,_Tname from _class p1,_teacher_class p2,_teacher p3,_student
            where p1._Cid=p2._Cid and p2._Tid=p3._Tid 
            group by p1._Cid,_Cname,p3._Tid,_Tname
            order by p1._Cid
            */
            string sqlStr_main = "select p1._Cid,_Cname,p3._Tid,_Tname from _class p1,_teacher_class p2,_teacher p3,_student where p1._Cid=p2._Cid and p2._Tid=p3._Tid group by p1._Cid,_Cname,p3._Tid,_Tname  order by p1._Cid";
            data_operate_in_admin.Data_bind(sqlStr_main, GridView_main, "_Cid");   //数据绑定GridView_main


            /*
            string sqlStr = "select _class._Cid,_class._Cname,_teacher._Tid,_Tname,_Tsex,_Tphone,_Sid,_Sname,_Ssex,_Sphone from _class,_student,_teacher where _teacher._Tid='" + "22012000010001" + "' and _class._Cid='" + "0000" + "' and _class._Cid=_student._SCid";
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Integrated Security=SSPI;initial catalog=mei_design;Server=USER-CQ3KP08JVH\\SQLEXPRESS;";
            SqlDataAdapter da = new SqlDataAdapter(sqlStr, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.GridView1.DataSource = ds;
            GridView1.DataBind();
             * */

            //Label14.Text = data_operate_in_admin.dataread("_teacher", "_Tname", "_Tid", id_teacherid.SelectedItem.Text);  //在TEXEBOX上显示选取的字段
        }
       // Label label = (Label)this.GridView_main.FindControl("Label14");
       // label.Text = DateTime.Now.ToShortDateString();
 
       // this.Label4.Text = data_operate_in_class_add.dataread("_teacher", "_Tname", "_Tid", id_teacherid.SelectedItem.Text);  //在TEXEBOX上显示选取的字段 
    }




    protected void gridview_main_editing(object sender, GridViewEditEventArgs e)     //编辑模式
    {
        GridView_main.EditIndex = e.NewEditIndex;   //进入编辑模式
        string sqlStr = "select p1._Cid,_Cname,p3._Tid,_Tname from _class p1,_teacher_class p2,_teacher p3,_student where p1._Cid=p2._Cid and p2._Tid=p3._Tid group by p1._Cid,_Cname,p3._Tid,_Tname  order by p1._Cid";
        data_operate_in_admin.Data_bind(sqlStr, GridView_main, "_Cid");   //数据绑定GridView_main

        frist_teacherid.Text = ((DropDownList)GridView_main.Rows[e.NewEditIndex].Cells[1].FindControl("id_teacherid")).Text;  //获得当前行教师最初的教师号。后面用


        CheckBox1.Checked = false;
        CheckBox2.Checked = false;
    }
     
    protected void gridView_main_Update(object sender, GridViewUpdateEventArgs e)    //更新事件
    {


        CheckBox1.Checked = false;
        CheckBox2.Checked = false;

        string data_key = GridView_main.DataKeys[e.RowIndex].Value.ToString();         //获取当前行主键id
        GridViewRow gvr = GridView_main.Rows[e.RowIndex];                              //实例化一行


        string str_classname = ((TextBox)gvr.FindControl("id_classname")).Text;          //获取textbox里面的值
        string change_str_id_teacherid = ((DropDownList)gvr.FindControl("id_teacherid")).Text;
        if (data_operate_in_admin.Data_update("_class", "_Cname", str_classname, "_Cid", data_key))//更新班级表里班级的名称
        {
            if(data_operate_in_admin.Data_update("_teacher_class", "_Tid", change_str_id_teacherid, "_Cid", data_key, "_Tid", frist_teacherid.Text))//更新班级教师表里老师的ID
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('更新成功');", true);
                frist_teacherid.Text = change_str_id_teacherid;
                return;
            }
        }
        Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('更新失败');", true);
    }

    protected void gridview_main_cancel(object sender, GridViewCancelEditEventArgs e)   //取消事件
    {

        CheckBox1.Checked = false;
        CheckBox2.Checked = false;

        GridView_main.EditIndex = -1;                                         //后退一页
        string sqlStr = "select p1._Cid,_Cname,p3._Tid,_Tname from _class p1,_teacher_class p2,_teacher p3,_student where p1._Cid=p2._Cid and p2._Tid=p3._Tid group by p1._Cid,_Cname,p3._Tid,_Tname  order by p1._Cid";
        data_operate_in_admin.Data_bind(sqlStr, GridView_main, "_Cid");   //数据绑定GridView_main                
    }


    protected void gridview_main_select(object sender, GridViewSelectEventArgs e)     //选择后进入详细列表
    {
        Button4.Visible = true;
        CheckBox1.Checked = false;
        CheckBox2.Checked = false;

        GridView_main.Visible = false;
        GridView_detail.Visible = true;
        static_Is_in_maingridview.Text = "当前是详细表";
      string str_classid = GridView_main.DataKeys[e.NewSelectedIndex].Value.ToString();         //获取班级ID
       GridViewRow gvr = GridView_main.Rows[e.NewSelectedIndex];                              //实例化一行
      string str_teacherid = ((Label)gvr.FindControl("Label3")).Text;          //获取教师ID


     // str_classid = str_classid.Replace(" ", "");   //去掉空格   。奇怪的问题。不知道数据库为什么出现了要去掉空格
     // str_teacherid = str_teacherid.Replace(" ", "");   //去掉空格   。奇怪的问题。不知道数据库为什么出现了要去掉空格

        //sql语句
        /*
         select _class._Cid,_class._Cname,_teacher._Tid,_Tname,_Tsex,_Tphone,_Sid,_Sname,_Ssex,_Sphone 
         from _class,_student,_teacher
         where _teacher._Tid='22012000010002' and _class._Cid='0000' and _class._Cid=_student._SCid
        */
      string sql = "select _class._Cid,_class._Cname,_teacher._Tid,_Tname,_Tsex,_Tphone,_Sid,_Sname,_Ssex,_Sphone from _class,_student,_teacher where _teacher._Tid='" + str_teacherid + "' and _class._Cid='" + str_classid + "' and _class._Cid=_student._SCid";
      data_operate_in_admin.Data_bind(sql, GridView_detail, "_Cid");//数据绑定
      //this.GridView_main.Visible = false;

      detail_classid.Text = str_classid;
            detail_teacherid.Text=str_teacherid;

        /*
       // string xxdm = GridView_main.DataKeys[e.NewSelectedIndex].Value.ToString();
        string sqlStr = "select _class._Cid,_class._Cname,_teacher._Tid,_Tname,_Tsex,_Tphone,_Sid,_Sname,_Ssex,_Sphone from _class,_student,_teacher where _teacher._Tid='" + str_classid + "' and _class._Cid='" + str_teacherid + "' and _class._Cid=_student._SCid";
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Integrated Security=SSPI;initial catalog=mei_design;Server=USER-CQ3KP08JVH\\SQLEXPRESS;";
        SqlDataAdapter da = new SqlDataAdapter(sqlStr, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.GridView1.DataSource = ds;
        GridView1.DataBind();
       // GridView_main.Visible = false;
         * */
    }

    protected void gridview_detail_select(object sender, GridViewSelectEventArgs e)
    {
      
    }
    protected void id_teacherid_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drp = sender as DropDownList; // 触发事件的 DropDownList
　　　  GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 当前行　即时在dropdownlist所在容器里 就是行的信息　　　　
　　　　row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
　　　　DropDownList ddlClass = (DropDownList)sender;
　　　  string　teacherid = ddlClass.SelectedValue.ToString();//获取Dropdownlist中选定值
        string teachername = data_operate_in_admin.dataread("_teacher", "_Tname", "_Tid", teacherid);
        ((Label)row.FindControl("Label4")).Text = teachername;
    }


    protected void checkbos_all_checkedchanged(object sender, EventArgs e)
    {
        if (        static_Is_in_maingridview.Text == "当前是主表")    //主表的全选操作  。。
        {
            for (int i = 0; i <= GridView_main.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)GridView_main.Rows[i].FindControl("CheckBox");
                if (CheckBox1.Checked == true)
                {
                    CheckBox.Checked = true;
                }
                else
                {
                    CheckBox.Checked = false;
                }
            }
            CheckBox2.Checked = false;
        }
        else   //详细表
        {
            for (int i = 0; i <= GridView_detail.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)GridView_detail.Rows[i].FindControl("CheckBox");
                if (CheckBox1.Checked == true)
                {
                    CheckBox.Checked = true;
                }
                else
                {
                    CheckBox.Checked = false;
                }
            }
            CheckBox2.Checked = false;
        }
    }
    protected void checkbos_Invert_checkedchanged(object sender, EventArgs e)
    {
        if (static_Is_in_maingridview.Text == "当前是主表")    //主表的全选操作  。。
        {
            for (int i = 0; i <= GridView_main.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)GridView_main.Rows[i].FindControl("CheckBox");
                if (CheckBox.Checked == false)
                {
                    CheckBox.Checked = true;
                }
                else
                {
                    CheckBox.Checked = false;
                }
            }
            CheckBox1.Checked = false;
        }
        else   //详细表
        {
            for (int i = 0; i <= GridView_detail.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)GridView_detail.Rows[i].FindControl("CheckBox");
                if (CheckBox.Checked == false)
                {
                    CheckBox.Checked = true;
                }
                else
                {
                    CheckBox.Checked = false;
                }
            }
            CheckBox1.Checked = false;
        }
    }
    protected void Button4_Click(object sender, EventArgs e)   //返回按钮
    {

        CheckBox1.Checked = false;
        CheckBox2.Checked = false;
        for (int i = 0; i <= GridView_main.Rows.Count - 1; i++)
        {
            CheckBox CheckBox = (CheckBox)GridView_main.Rows[i].FindControl("CheckBox");
            if (CheckBox.Checked == false)
            {
                CheckBox.Checked = true;
            }
            else
            {
                CheckBox.Checked = false;
            }
        }


        static_Is_in_maingridview.Text = "当前是主表";
        GridView_main.Visible = true;
        GridView_detail.Visible = false;

        Button4.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)   //删除
    {
        if (static_Is_in_maingridview.Text == "当前是主表")    //主表的删除操作  。。主要用于删除整个班级
        {
            int j = 0;//判断班级是否全部删完
            int count = GridView_main.Rows.Count;//一开始总共的列数
            for (int i = 0; i <= GridView_main.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)GridView_main.Rows[i].FindControl("CheckBox");
                if (CheckBox.Checked == true)
                {
                    string str_condition_info = this.GridView_main.DataKeys[i].Value.ToString();     //获得主表字段

                    string str_teacherid = ((Label)GridView_main.Rows[i].FindControl("Label3")).Text;  
              
    

               

                    string sql_delete_class = @"delete from _teacher_class where _Cid='"+str_condition_info+"' and _Tid='"+str_teacherid+"'";
                    if (data_operate_in_admin.Is_sqlcmd_success(data_operate_in_admin.sqlcmd(sql_delete_class))) 
                    {
                       // Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('删除成功');", true);
                        Label5.Visible = true;
                        Label5.Text = "班级" + str_condition_info + "删除成功";
                    }
                    else
                    {
                        Label5.Visible = true;
                        Label5.Text = "班级" + str_condition_info + "删除失败";
                    }
                    j++;
                }

            }
            Response.Redirect(Request.RawUrl);
            /*
            if (count == j)
            {
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('没有数据');", true);
                Label2.Text = "2";//弹出所有数据都删除完了
                Page_Load(sender, e);
                // Response.Write("<script>alert('删除成功!');window.location.href ='www.cnblogs.com'</script>");
            }
            else
            {
                Response.Redirect(Request.RawUrl);
            }
             */
        }
        else
        {
            for (int i = 0; i <= GridView_detail.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)GridView_detail.Rows[i].FindControl("CheckBox");
                if (CheckBox.Checked == true)
                {                
                    string data_key = GridView_detail.DataKeys[i].Value.ToString();         //获取当前行主键id     
                    GridViewRow gvr = GridView_detail.Rows[i];
                    string str_studentid = ((Label)gvr.FindControl("dLabel_studentid")).Text;
                    data_operate_in_admin.datadelete("_student_homework", "_Sid", str_studentid); 
                    data_operate_in_admin.datadelete("_student", "_Sid", str_studentid);   //删除班级的单个学生信息    
                }
            }
            Response.Redirect(Request.RawUrl);
        }

    }
    protected void Button5_Click(object sender, EventArgs e)    //显示全部班级信息
    {

    }
    protected void gridview_main_pageindexchanging(object sender, GridViewPageEventArgs e)
    {
        GridView_main.PageIndex = e.NewPageIndex;
        string sqlStr_main = "select p1._Cid,_Cname,p3._Tid,_Tname from _class p1,_teacher_class p2,_teacher p3,_student where p1._Cid=p2._Cid and p2._Tid=p3._Tid group by p1._Cid,_Cname,p3._Tid,_Tname  order by p1._Cid";
        data_operate_in_admin.Data_bind(sqlStr_main, GridView_main, "_Cid");  

    }
    protected void gridview_detail_pageindexchanging(object sender, GridViewPageEventArgs e)
    {

        string sql = "select _class._Cid,_class._Cname,_teacher._Tid,_Tname,_Tsex,_Tphone,_Sid,_Sname,_Ssex,_Sphone from _class,_student,_teacher where _teacher._Tid='" + detail_teacherid.Text + "' and _class._Cid='" + detail_classid.Text + "' and _class._Cid=_student._SCid";
        data_operate_in_admin.Data_bind(sql, GridView_detail, "_Cid");//数据绑定
      
    }
}